<?php

namespace api\services\goods;

use api\extensions\ApiBaseService;
use common\models\goods\cate\GoodsCateModel;
use common\services\cache\CommonCache;

/**
 * Desc 分类管理服务类
 * @author WMX
 */
class GoodsCateService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($refresh = false)
    {
        try {
            $mkey = 'CATE_LIST_';
            $data = CommonCache::getCache($mkey);
            if ($data && empty($refresh)) {
                // return self::jsonSuccess($data);
            }
            $cate = GoodsCateModel::find()
                ->select(['id', 'name', 'img', 'url', 'pid'])
                ->where(['state' => GoodsCateModel::STATE_ENABLE])
                ->orderBy(['sort' => SORT_ASC])
                ->asArray()
                ->all();
            if (empty($cate)) {
                throw new \Exception('分类数据异常');
            }
            $tree = GoodsCateModel::arrayTree($cate);
            CommonCache::setCache($mkey, $tree);
            return self::jsonSuccess($tree);
        } catch (\Exception $exc) {
            return self::jsonSuccess([]);
        }
    }
}
